from app.extensions import SessionLocal
from app.models.Clas import Clas
from app.repositories.StudentRepo import StudentRepository
from app.repositories.ClasRepo import ClasRepository
from app.asserts import code
from fastapi import APIRouter, Response, status
from app.schemas.Student import StudentSchema, StudentUpdate


class StudentService():
    def __init__(self, db: SessionLocal):
        # 创建DAO层对象
        self.repo = StudentRepository(db)
        self.clas_repo = ClasRepository(db)

    def get_by_keyword(self, keyword):
        """
        根据关键字查询数据
        :param keyword: 关键字
        :return: 如果查询成功返回数据，查询失败返回空列表
        """
        data = self.repo.get_by_keyword(keyword)
        return {
            "code": status.HTTP_200_OK,
            "data": data,
            "message": "查询成功"
        }

    def get_by_id(self, student_id):
        """
        根据ID查询学生
        :param student_id: 学生ID
        :return: 查询结果字典
        """
        student = self.repo.get_by_id(student_id)
        if student:
            return {
                "code": status.HTTP_200_OK,
                "data": student,
                "message": "查询成功"
            }
        return {
            "code": status.HTTP_404_NOT_FOUND,
            "data": None,
            "message": "学生不存在"
        }

    def add_student(self, clas_id, name, gender):
        # 检查clasid存在吗？
        clas = self.clas_repo.get_clas_by_id(clas_id)
        if clas is None:
            return {
                "code": status.HTTP_404_NOT_FOUND,
                "data": None,
                "message": "班级不存在"
            }
        # 增加学生
        result = self.repo.add_student(int(clas_id), name,gender)
        if result:
            return {
                "code": status.HTTP_200_OK,
                "data": None,
                "message": "学生已创建成功"
            }

    def update_student(self, student_id: int, student_update: StudentUpdate):
    #     判断student id是否存在，获取student
        stu = self.repo.get_student_by_id(student_id)
        if stu is None:
            return {
                "code": status.HTTP_404_NOT_FOUND,
                "data": None,
                "message": "学生不存在"
            }
        # 如果 student_update 中有 clas_id，检查班级是否存在
        new_clas_id = None
        if student_update.clas_id is not None:
            clas = self.clas_repo.get_clas_by_id(student_update.clas_id)
            if clas is None:
                return {
                    "code": code.NOT_FOUND,
                    "data": None,
                    "message": "指定的班级不存在"
                }
        new_clas_id = student_update.clas_id
        new_name = student_update.name if student_update.name is not None else stu.name
        new_gender = student_update.gender if student_update.gender is not None else stu.gender
        result = self.repo.update_student(student_id, new_name, new_gender, new_clas_id)
        if result:
            return {
                "code": status.HTTP_200_OK,
                "data": None,
                "message": "修改成功"
            }
        return {
            "code": status.HTTP_500_INTERNAL_SERVER_ERROR,
            "data": None,
            "message": "修改失败"
        }



    def delete_student(self, student_id):
        student = self.repo.get_by_id(student_id)
        if not student:
            return {
                "code": status.HTTP_404_NOT_FOUND,
                "data": None,
                "message": "学生不存在"
            }

        result = self.repo.delete_student(student_id)
        if result:
            return {
                "code": status.HTTP_200_OK,
                "data": None,
                "message": "删除成功"
            }
        else:
            return {
                "code": status.HTTP_500_INTERNAL_SERVER_ERROR,
                "data": None,
                "message": "删除失败"
            }